package com.zzx.project.controller;

import com.zzx.project.entity.User;
import com.zzx.project.entity.Websitelm;
import com.zzx.project.service.UserService;
import com.zzx.project.service.WlmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

//网站留言Controller
@Controller
@RequestMapping("wlm")
public class WlmController {

    @Autowired
    WlmService wlmService;
    @Autowired
    UserService userService;

    //添加留言
    @RequestMapping("add")
    public String add(Websitelm websitelm, HttpSession session, Model model) {
        Random random = new Random();
        int num = random.nextInt(5) + 1;
        switch (num) {
            case 1:
                websitelm.setLmimg("/userImages/1.gif");
                break;
            case 2:
                websitelm.setLmimg("/userImages/2.gif");
                break;
            case 3:
                websitelm.setLmimg("/userImages/3.gif");
                break;
            case 4:
                websitelm.setLmimg("/userImages/4.gif");
                break;
            case 5:
                websitelm.setLmimg("/userImages/5.gif");
                break;
        }
        int flag = wlmService.addWlm(websitelm);
        if (flag > 0) {
            List<User> users = userService.getAll();
            model.addAttribute("users", users);
            model.addAttribute("data", wlmService.getAllData());
            model.addAttribute("refresh", 1);
            session.setAttribute("data", wlmService.getFiveData());
            return "main/readerWall";
        } else {
            System.err.println("出现添加错误，请检查");
            return "error/5xx";
        }
    }
}
